<?php include("includes/session.php"); ?>
<?php include("includes/connect_db.php"); ?>
<?php include("includes/header.php"); ?>
<?php include("notif.php"); ?>

<?php
	$id_inbox = $_GET['id'];
	$id_outbox = $_GET['id'];
	$aksi = $_GET['aksi'];

	if ($aksi == 1) {
		$sql_cari_kontak = "SELECT SenderNumber FROM inbox WHERE ID = '$id_inbox'";
		$query_cari_kontak = mysql_query($sql_cari_kontak);
		$cari_kontak = mysql_fetch_array($query_cari_kontak);
		$nomor_get = $cari_kontak['SenderNumber'];
	} else if ($aksi == 2) {
		$sql_cari_kontak = "SELECT DestinationNumber FROM sentItems WHERE ID = '$id_outbox'";
		$query_cari_kontak = mysql_query($sql_cari_kontak);
		$cari_kontak = mysql_fetch_array($query_cari_kontak);
		$nomor_get = $cari_kontak['DestinationNumber'];
	}
?>

<style>
	.frame{
		border: 1px solid #08c;
		width: 250px;
		height: 250px;
		margin-bottom: 5px;
	}

	#pilihGambar{
		width: 250px;
		text-align: center;
	}
	.btn{
		width: 150px;
	}
</style>

<div class="span9">  
    <h3>SMS</h3>                      
    <ul class="breadcrumb well">
    	<li><a href="kontak.php">Contact</a> <span class="divider">/</span></li>
        <li class="active">Tambah</li>                
    </ul>    
    <div class="alert alert-info">
		Tambah Kontak
    </div>
    
	<form class="form-horizontal">
    	<div>
			<div class="span12">
				<div class="span8">
					<div class="control-group">
						<label class="control-label">Nama</label>
						<div class="controls">
							<input placeholder="Masukkan Nama" type="text" name="nama" value="<?php echo $_POST['nama'] ?>">
						</div>
					</div>

					<div class="control-group">
						<label class="control-label">Email</label>
						<div class="controls">
							<input placeholder="Masukkan Email" type="email" name="email" value="<?php echo $_POST['email'] ?>">
						</div>
					</div>
					
					<div class="control-group">
						<label class="control-label">Jenis Kelamin</label>
						<div class="controls">
					    	<input type="radio" value="l" name="jenis_kelamin"> Laki-Laki
					    	<input type="radio" value="p" name="jenis_kelamin"> Perempuan
						</div>
					</div>

					<div class="control-group">
						<label class="control-label">Alamat</label>
						<div class="controls">
							<textarea placeholder="Masukkan Alamat" name="alamat" style="height: 50px; overflow: hidden; word-wrap: break-word; resize: none;"><?php echo $_POST['alamat'] ?></textarea>
						</div>
					</div>
					
					<div class="control-group">
						<label class="control-label">Tanggal Lahir</label>
						<div class="controls">
							<input placeholder="Masukkan Tanggal Lahir" type="date" name="tanggal" value="<?php echo $_POST['tanggal'] ?>" class="datePicker">
						</div>
					</div>
					
					<div class="control-group">
						<label class="control-label">No. Telepon</label>
						<div class="controls">
							<?php
								if ($aksi == 1 OR $aksi == 2) {
							?>
								<input type="text" name="nomor" value="<?php echo $nomor_get; ?>" readonly>
							<?php
								} else {
							?>
								<input placeholder="Masukkan Nomor Telepon" type="text" name="nomor" value="<?php echo $_POST['nomor'] ?>">
							<?php
								}
							?>
						</div>
					</div>
					
					<div class="control-group">
						<div class="controls">
							<button class="btn btn-primary tambahNomor"><i class="icon-white icon-plus"></i>Tambah Nomor</button>
						</div>
					</div>
				</div>
			</div>
			<div>
				<div class="span12">
					<ul class="breadcrumb well">
						<button class="btn btn-info" name="simpan"><i class="icon-white icon-ok"></i> Simpan</button>
						<button class="btn btn-danger" name="batal"><i class="icon-white icon-remove"></i> Batal</button>
					</ul>
				</div>
			</div>
		</div>
	</form>
</div>

<?php
	if(isset($_POST['simpan'])){
		$nama = $_POST['nama'];
		$nomor = $_POST['nomor'];
		$email = $_POST['email'];
		$jenis_kelamin = $_POST['jenis_kelamin'];
		$tanggal = $_POST['tanggal'];
		$image = $_POST['image'];
		$alamat = $_POST['alamat'];

		//echo $nama."; ".$nomor."; ".$email."; ".$jenis_kelamin."; ".$tanggal."; ".$image."; ".$alamat;

		if (empty($nama) or empty($nomor)){
            echo "<SCRIPT> alert('ISI DATA TERLEBIH DAHULU');</SCRIPT>";
        } else{

        	if (is_numeric($nomor)) {
				if ($nomor[0] == '0') {
					$panjang = strlen($nomor);
					if ($panjang <= 12) {
						$nomor = "+62".substr($nomor, 1, $panjang-1);
					}
				} elseif ($nomor[0] == '+' and $nomor[1] == '6' and $nomor[2] == '2') {
					$panjang = strlen($nomor);

					$nomorBaru = substr($nomor, 3, $panjang-3);
					$panjang2 = strlen($nomorBaru);
					$nomorBaru2 = (int)$nomorBaru;
					$panjang3 = strlen($nomorBaru2);

					if ($panjang2 <= 12) {
						if ($panjang2 == $panjang3) {
							$nomor = "+62".$nomorBaru;
						}
					}
				}
			} 

        	if (empty($tanggal) and empty($jenis_kelamin)) {
        		$sql_input_kontak = "INSERT INTO tb_kontak (nama_kontak, no_hp, email, alamat) 
									VALUES ('$nama', '$nomor', '$email', '$alamat')";
			} elseif (empty($tanggal)){
        		$sql_input_kontak = "INSERT INTO tb_kontak (nama_kontak, no_hp, email, jenis_kelamin, alamat) 
									VALUES ('$nama', '$nomor', '$email', '$jenis_kelamin', '$alamat')";
        	} elseif (empty($jenis_kelamin)) {
        		$sql_input_kontak = "INSERT INTO tb_kontak (nama_kontak, no_hp, email, tanggal_lahir, alamat) 
									VALUES ('$nama', '$nomor', '$email', '$tanggal', '$alamat')";
        	} else {
	        	$sql_input_kontak = "INSERT INTO tb_kontak (nama_kontak, no_hp, email, jenis_kelamin, tanggal_lahir, alamat) 
									VALUES ('$nama', '$nomor', '$email', '$jenis_kelamin', '$tanggal', '$alamat')";
        	}

			$query_input_kontak = mysql_query($sql_input_kontak) or die(mysql_error());

			if ($query_input_kontak) {
				echo "<SCRIPT> alert('DATA DISIMPAN'); location.href='kontak.php';</SCRIPT>";
			} else {
				echo "<SCRIPT> alert('DATA GAGAL DISIMPAN');</SCRIPT>";
			}
	  	}
	} elseif (isset($_POST['batal'])) {
		echo "<SCRIPT> location.href='kontak.php';</SCRIPT>";
	}
?>

<?php include("includes/footer.php"); ?>

<script type="text/javascript">
	$(document).ready(function() {
		
	});
</script>